package com.steak.concurrent.CompletableFuture.api.call;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author 刘牌
 * @Title:
 * @date 2022-04-2312:19
 */
public class And {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> task = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 100;
        });

        CompletableFuture<String> combine = CompletableFuture.supplyAsync(() -> 100).thenCombineAsync(task, (s, th) -> {
            try {
                System.out.println("task  " + task.get());
                System.out.println(s);
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            return "thenCombine";
        });
        System.out.println(combine.get());


        CompletableFuture<Void> bothAsync = CompletableFuture.supplyAsync(() -> 100).thenAcceptBothAsync(task, (s, th) -> {
            try {
                System.out.println(task.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
        System.out.println("bothAsync  "+bothAsync.get());

    }
}
